package com.ying.controller.order;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ApiResponse;
import com.ying.po.order.Express;
import com.ying.service.order.ExpressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/order/express")
@Api(tags = "Express接口")
public class ExpressController {

    @Autowired
    private ExpressService expressService;

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询Express")
    @SaCheckPermission("express-get")
    public ApiResponse finById(@PathVariable(value = "id") Integer id) {
        return ApiResponse.success(expressService.findById(id));
    }

    @GetMapping("/sync")
    @SaCheckPermission("express-put")
    @ApiOperation("同步system的快递数据")
    public ApiResponse syncSystemData(@RequestParam("userId") Integer userId) {
        expressService.syncSystemData(userId);
        return ApiResponse.success();
    }

    @GetMapping("/restore")
    @ApiOperation("恢复数据到system")
    @SaCheckPermission("express-put")
    public ApiResponse restoreToSys(@RequestParam("userId") Integer userId) {
        List<Express> expressList = expressService.restoreToSys(userId);
        return ApiResponse.success(expressList);
    }

    // √
    @PostMapping("/create")
    @ApiOperation("创建Express对象")
    @SaCheckPermission("express-post")
    public ApiResponse create(@RequestBody Express express) {
        // 存储Express
        Integer id = expressService.create(express);
        return ApiResponse.success(id);
    }

//    @PostMapping("/page")
//    @ApiOperation("分页查询")
//    public ApiResponse findPage(@RequestBody PageWrap<Express> pageWrap) {
//        return ApiResponse.success(expressService.findAllByPage(pageWrap));
//    }
   // √
    @PutMapping("/update")
    @ApiOperation("修改Express")
    @SaCheckPermission("express-put")
    public ApiResponse updateById(@RequestBody Express express) {
        expressService.updateById(express);
        return ApiResponse.success(null);
    }

    // √
    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除")
    @SaCheckPermission("express-delete")
    public ApiResponse delete(@PathVariable Integer id) {
        expressService.deleteById(id);
        return ApiResponse.success(null);
    }

    // √
    @DeleteMapping("/delete")
    @ApiOperation("根据ID删除")
    @SaCheckPermission("express-delete")
    public ApiResponse deleteByList(@RequestBody List<Integer> ids) {
        expressService.deleteByIdInBatch(ids);
        return ApiResponse.success(null);
    }

//    @GetMapping("/countall")
//    @ApiOperation("查询数量")
//    public ApiResponse countAll() {
//        long count = expressService.countAll();
//        return ApiResponse.success(count);
//    }

//    @PostMapping("/findbyids")
//    @ApiOperation("根据ID列表查询")
//    public ApiResponse findByIds(@RequestBody Integer[] ids) {
//        List<Express> userList = expressService.findByIdInBatch(Arrays.asList(ids));
//        return ApiResponse.success(userList);
//    }

    @GetMapping("/find/all")
    @ApiOperation("查询全部")
    @SaCheckPermission("express-get")
    public ApiResponse findAll(@RequestParam("userId") Integer userId) {
        ApiResponse success = ApiResponse.success(expressService.findAll(userId));
        return success;
    }


    @GetMapping("/name/all")
    @ApiOperation("查询全部的express name")
    @SaCheckPermission("express-get")
    public ApiResponse getAllSellerName(@RequestParam("userId") Integer userId) {
        return ApiResponse.success(expressService.getAllExpressName(userId));
    }

    @PostMapping("/query")
    @ApiOperation("根据条件查询Express")
    @SaCheckPermission("express-get")
    public ApiResponse queryAllByCondition(@RequestBody Express express) {
        return ApiResponse.success(expressService.queryAllByCondition(express));
    }

}
